<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

class DcatPatchCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dcat:patch';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $this->info("==================== 开始安装 =====================");
        $start = microtime(true);
        $this->publish();
//        $this->authority();
        $end = microtime(true);
        $t = round($end - $start);
        $this->info("================ 配置生成结束，运行{$t}秒 =================");
    }

    /**
     * public config
     *
     * @return void
     */
    public function publish()
    {
        File::copyDirectory(base_path("vendorM"), base_path("vendor"));
    }

    /**
     * 权限添加
     */
    public function authority()
    {
        DB::update('delete from admin_permissions where id>6');
        DB::update("ALTER TABLE admin_permissions AUTO_INCREMENT = 7");
        DB::table('admin_permissions')->insertGetId(
            ['name' => '首页', 'slug' => 'dashboard', 'http_path' => 'dashboard', 'http_method' => '', 'order' => 0]);

        $result = DB::table('admin_menu')->where('id', '>', 8)->get()->toArray();
        $newResult = [];
        foreach ($result as $k => $v) {
            $newResult[] = json_decode(json_encode($v), true);
        }
        $pid = 0;
        $treeList = $this->create_tree_list($pid, $newResult);

        //最多三层（目前2层）
        foreach ($treeList as $k => $v) {
            if (isset($v['children'])) {
                $insertOne = $this->format($v);
                $insertOne['parent_id'] = 0;
                $oneParentId = DB::table('admin_permissions')->insertGetId($insertOne);
                foreach ($v['children'] as $kk => $vv) {
                    $insertTwo = $this->format($vv);
                    $insertTwo['parent_id'] = $oneParentId;
                    if (isset($vv['children'])) {
                        unset($insertTwo['children']);
                        $twoParentId = DB::table('admin_permissions')->insertGetId($insertTwo);
                        foreach ($vv['children'] as $kkk => $vvv) {
                            $insertTree = $this->format($vvv);
                            $insertTree['parent_id'] = $twoParentId;
                            DB::table('admin_permissions')->insertGetId($insertTree);
                        }
                    } else {
                        DB::table('admin_permissions')->insertGetId($insertTwo);
                    }
                }
            } else {
                DB::table('admin_permissions')->insertGetId($v);
            }
        }
    }

    public function format($v)
    {
        $insertOne['name'] = $v['title'];
        $insertOne['slug'] = str_replace("/", "", $v['uri']) ?: $v['title'];
        $insertOne['http_method'] = '';
        $insertOne['http_path'] = str_replace("/", "", $v['uri']) . "*";
        $insertOne['order'] = 0;
        return $insertOne;
    }

    public function create_tree_list($pid, $arr, &$tree = [])
    {
        foreach ($arr as $key => $vo) {
            if ($vo['parent_id'] == $pid) {
                $c = $this->create_tree_list($vo['id'], $arr);
                if ($c) {
                    $vo['children'] = $c;
                }
                $tree[] = $vo;
            }
        }
        return $tree;
    }

}
